package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api (tags = "c端地址管理")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    @ApiOperation("查询当前用户的所有地址信息")
    public Result<List<AddressBook>> getAllAddress(){
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.getAllAddressBook(addressBook);
        return Result.success(list);
    }

    @PostMapping
    @ApiOperation("新增地址信息")
    public Result addAddressBook(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.saveAddressBook(addressBook);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改地址信息")
    public Result updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据地址id查询地址信息")
    public Result<AddressBook> getAddressBookById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getAddressBookById(id);
        return Result.success(addressBook);
    }

    @DeleteMapping
    @ApiOperation("根据地址id删除地址信息")
    public Result deleteAddressBookById(Long id) {
        addressBookService.deleteAddressBookById(id);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefaultAddress(@RequestBody AddressBook addressBook) {
        addressBookService.updateDefault(addressBook);
        return Result.success();
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefaultAddress() {
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook = addressBookService.getDefaultAddress(addressBook);
        return Result.success(addressBook);
    }
}
